#include <stdio.h>
#include <cassert>
#include "Piece.h"
#include "Cell.h"
#include "Board.h"

Cell::Cell() {
	board = NULL;
	piece = NULL;
	row = -1;
	col = -1;
}
Cell::Cell(Board* board, int row, int col) : 
		board(board), row(row), col(col) {

	piece = NULL;
	
}
Cell::~Cell() {
	
}

void Cell::BindToPiece(Piece* piece) {
	this->piece = piece;
}

Piece* Cell::GetPiece() {
	return piece;
}
int Cell::GetRow() {
	return row;
}
int Cell::GetCol() {
	return col;
}

std::string Cell::toString(){
	std::string output = "<piece";
	char col[21]; // enough to hold all numbers up to 64-bits
	sprintf(col, "%d", piece->GetCol());
	char row[21]; // enough to hold all numbers up to 64-bits
	sprintf(row, "%d", piece->GetRow());

	output += " type=\""+ piece->getTypeString();
	output += "\" color=\""+ piece->getColorString();
	output += "\" column=\"";
	output += col;
	output += "\" row=\"";
	output += row;
	output += "\" />";
	return output;
}





Cell* Cell::FdLeft(PieceColor color) {
	assert(this != NULL);
	
	if (Fd(color) != NULL) {
		return Fd(color)->Left(color);
	}
	else return NULL;
}
Cell* Cell::Fd(PieceColor color) {
	assert(this != NULL);
	assert(0 <= row && row <= 7);
	assert(0 <= col && col <= 7);
	
	if (color == WHITE) {
		if (row > 0)
			return board->GetCell( row-1,col );
		else return NULL;
	}
	else {
		if (row < 7)
			return board->GetCell( row+1,col );
		else return NULL;
	}
}
Cell* Cell::FdRight(PieceColor color) {
	assert(this != NULL);
	
	if (Fd(color) != NULL) {
		return Fd(color)->Right(color);
	}
	else return NULL;
}
Cell* Cell::Left(PieceColor color) {
	assert(this != NULL);
	assert(0 <= row && row <= 7);
	assert(0 <= col && col <= 7);
	
	if (color == WHITE) {
		if (col > 0)
			return board->GetCell( row,col-1 );
		else return NULL;
	}
	else {
		if (col < 7)
			return board->GetCell( row,col+1 );
		else return NULL;
	}
}
Cell* Cell::Right(PieceColor color) {
	assert(this != NULL);
	assert(0 <= row && row <= 7);
	assert(0 <= col && col <= 7);
	
	if (color == WHITE) {
		if (col < 7)
			return board->GetCell( row,col+1 );
		else return NULL;
	}
	else {
		if (col > 0)
			return board->GetCell( row,col-1 );
		else return NULL;
	}
}
Cell* Cell::BkLeft(PieceColor color) {
	assert(this != NULL);
	
	if (Bk(color) != NULL) {
		return Bk(color)->Left(color);
	}
	else return NULL;
}
Cell* Cell::Bk(PieceColor color) {
	assert(this != NULL);
	assert(0 <= row && row <= 7);
	assert(0 <= col && col <= 7);
	
	if (color == WHITE) {
		if (row < 7)
			return board->GetCell( row+1,col );
		else return NULL;
	}
	else {
		if (row > 0)
			return board->GetCell( row-1,col );
		else return NULL;
	}
}
Cell* Cell::BkRight(PieceColor color) {
	assert(this != NULL);
	
	if (Bk(color) != NULL) {
		return Bk(color)->Right(color);
	}
	else return NULL;
}

